package com.lily.aidouzixun.bean;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.SparseArray;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 *  动态首页信息
 *
 */
public class DynamicHomeInfo implements Parcelable {

    /**
     * Json-key
     */
    private static final String CURPAGE_JOSANKEY = "curpage";
    private static final String TOTAL_JOSANKEY = "totalpage";
    private static final String DISCUSSLIST_JSONKEY = "discussList";

    /**
     * 动态列表
     */
    private int curpage;
    private int totalpage;
    private SparseArray<DynamicInfo> discussList;

    public int getCurpage() {
        return curpage;
    }

    public void setCurpage(int curpage) {
        this.curpage = curpage;
    }

    public int getTotalpage() {
        return totalpage;
    }

    public void setTotalpage(int totalpage) {
        this.totalpage = totalpage;
    }

    public SparseArray<DynamicInfo> getDiscussList() {
        return discussList;
    }
    public void setDiscussList(SparseArray<DynamicInfo> discussList) {
        this.discussList = discussList;
    }

    /**
     * 解析数据
     * @param jsonObj
     * @return 文章首页数据
     */
    public static DynamicHomeInfo parseInfo(JSONObject jsonObj) {
        if (jsonObj == null) {
            return null;
        }
        //解析分页信息
        DynamicHomeInfo discussHome = new DynamicHomeInfo();
        if (jsonObj.has(CURPAGE_JOSANKEY)) {
            discussHome.setCurpage(jsonObj.optInt(CURPAGE_JOSANKEY));
        }
        if (jsonObj.has(TOTAL_JOSANKEY)) {
            discussHome.setTotalpage(jsonObj.optInt(TOTAL_JOSANKEY));
        }
        if (jsonObj.has(DISCUSSLIST_JSONKEY)) {    //动态列表信息
            SparseArray<DynamicInfo> discussList = new SparseArray<DynamicInfo>();
            JSONArray discussArr = jsonObj.getJSONArray(DISCUSSLIST_JSONKEY);
            for (int i = 0; i<discussArr.size(); i++) {
                JSONObject discussJsonObj = discussArr.getJSONObject(i);
                DynamicInfo discussInfo = DynamicInfo.parseInfo(discussJsonObj);
                if (discussInfo != null) {
                    discussList.put(i, discussInfo);
                }
            }
            discussHome.setDiscussList(discussList);
        }
        return discussHome;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {

    }
}
